package com.aplink.generic.widgets;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.type.EnableConditionType;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.controlspec.ButtonSpec;
import com.aplink.generic.pojo.controlspec.ControlSpec;

public class ApLinkButton extends ApLinkBaseView {

    /**
     * 
     */
    private static final long serialVersionUID = 15843254635171940L;
    private final Button mButton;
    private final ButtonSpec mButtonSpec;

    public ApLinkButton(final GenericFragment dataEntryActivity,
            final ControlSpec controlSpec, final View view) {
        super(dataEntryActivity, controlSpec, view);
        this.mButton = (Button) view;
        this.mButtonSpec = (ButtonSpec) controlSpec;
    }

    @Override
    public void clear() {

    }

    @Override
    public GenericAttribute getAttribute() {
        return null;
    }

    @Override
    public GenericDictionary<String, GenericAttribute> getListAttributes() {
        return null;
    }

    @Override
    public Button getView() {
        return this.mButton;
    }

    @Override
    protected void initViewProperties() {
        this.mButton.setTag(this.mButtonSpec);
        if (getEnableCondition() != null) {
            final boolean isValid = getEnableCondition().isValid(
                    getBindingObject());
            final EnableConditionType validType = getEnableCondition()
                    .getValidType();
            final EnableConditionType invalidType = getEnableCondition()
                    .getInvalidType();

            if (isValid) {
                if (validType.equals(EnableConditionType.DISABLE)) {
                    this.mButton.setEnabled(false);
                } else if (validType.equals(EnableConditionType.ENABLE)) {
                    this.mButton.setEnabled(true);
                } else if (validType.equals(EnableConditionType.GONE)) {
                    this.mButton.setVisibility(View.GONE);
                } else if (validType.equals(EnableConditionType.VISIBLE)) {
                    this.mButton.setVisibility(View.VISIBLE);
                } else if (validType.equals(EnableConditionType.INVISIBLE)) {
                    this.mButton.setVisibility(View.INVISIBLE);
                }
            } else {
                if (invalidType.equals(EnableConditionType.DISABLE)) {
                    this.mButton.setEnabled(false);
                } else if (invalidType.equals(EnableConditionType.ENABLE)) {
                    this.mButton.setEnabled(true);
                } else if (invalidType.equals(EnableConditionType.GONE)) {
                    this.mButton.setVisibility(View.GONE);
                } else if (invalidType.equals(EnableConditionType.VISIBLE)) {
                    this.mButton.setVisibility(View.VISIBLE);
                } else if (invalidType.equals(EnableConditionType.INVISIBLE)) {
                    this.mButton.setVisibility(View.INVISIBLE);
                }
            }
        }
    }

    @Override
    protected void loadBindingObject() {
        if (getPassedContent() != null) {
            this.mButton.setText(getPassedContent());
        }
    }

    @Override
    protected void loadDefaultObject() {

    }

    @Override
    protected void setEventListener() {
        if (this.mButtonSpec.getOnClickListener() != null) {
            this.mButton.setOnClickListener(this.mButtonSpec
                    .getOnClickListener());
        } else {
            this.mButton.setOnClickListener(getGenericFragment());
        }
    }

    @Override
    public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }

    @Override
    public void setOnClickListener(final OnClickListener onClickListener) {
        this.mButton.setOnClickListener(onClickListener);
    }
}
